package com.study.data.controller;

import com.study.common.entity.ResultInfo;
import com.study.common.entity.User;
import com.study.common.pagination.Pagination;
import com.study.common.utils.I18nUtils;
import com.study.common.utils.ResultUtils;
import com.study.main.entity.Information;
import com.study.main.service.MainService;
import com.study.main.vo.ConditionPagination;
import com.study.main.vo.UserInfo;
import com.study.register.service.RegisterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Locale;

/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/9/4 15:03
 * @Description 获取数据控制器
 */
@Controller
@RequestMapping("/data")
public class dataController {
    @Autowired
    @Qualifier(value = "mainServiceImpl")
    private MainService mainService;
    @Autowired
    @Qualifier(value = "registerService")
    private RegisterService registerService;

    /**
     * 权限判断控制器
     *
     * @return
     */
    @RequiresPermissions("all")
    @RequestMapping("/a")
    @ResponseBody
    public ResultInfo a() {
        return new ResultUtils().success(null, "");
    }

    /**
     * 权限判断控制器(超级管理员)
     *
     * @return
     */
    @RequiresPermissions("super")
    @RequestMapping("/b")
    @ResponseBody
    public ResultInfo b() {
        return new ResultUtils().success(null, "");
    }

    /**
     * 获取用户信息
     *
     * @param session
     * @return com.study.common.entity.User
     */

    @RequestMapping("/userInfoTwo")
    @ResponseBody
    public User userInfoDataTwo(HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        return user;
    }

    /**
     * 分页
     *
     * @param conditionPagination 条件分页类
     * @return com.study.common.entity.ResultData<com.study.main.vo.UserInfo>
     */
    @RequestMapping("/userInfo")
    @ResponseBody
    public Pagination userInfoData(ConditionPagination conditionPagination) {

        Pagination pagination = mainService.getPageByCondition(conditionPagination);

        return pagination;
    }

    @RequestMapping("/userInfoOne")
    @ResponseBody
    public Pagination userInfoDataOne(@RequestBody ConditionPagination conditionPagination) {
        Pagination pagination = mainService.getPageByCondition(conditionPagination);
        return pagination;
    }

    /**
     * 根据id查询用户信息
     *
     * @param locale
     * @return com.study.common.entity.ResultInfo<com.study.common.entity.User>
     */
    @RequiresPermissions("all")
    @RequestMapping("/addSelect")
    @ResponseBody
    public ResultInfo<User> addDataSelect(Locale locale, @RequestBody ConditionPagination c) {
        try {
            return new ResultUtils<User>().success(mainService.getById(Integer.parseInt(c.getInfoId())),
                    "inquireSuccess");
        } catch (Exception e) {
            return new ResultUtils<User>().fail(I18nUtils.getrb(locale, "inquireFail"));
        }
    }

    /**
     * 根据infoId查询
     *
     * @param locale      路径
     * @param information 用户信息
     * @return com.study.common.entity.ResultInfo<com.study.main.vo.UserInfo>
     */
    @RequiresPermissions("all")
    @RequestMapping("/addSelectOne")
    @ResponseBody
    public ResultInfo<UserInfo> addDataSelect2(Locale locale, @RequestBody Information information) {
        try {
            return new ResultUtils<UserInfo>().success(mainService.getUserInfoById(information.getInfoId()),
                    I18nUtils.getrb(locale, "inquireSuccess"));
        } catch (Exception e) {
            return new ResultUtils<UserInfo>().fail(I18nUtils.getrb(locale, "inquireFail"));
        }
    }

    /**
     * 根据id查询用户 成绩学分
     *
     * @param locale
     * @param user   用户信息
     * @return java.util.List<com.study.main.entity.Information>
     */
    @RequiresPermissions("all")
    @RequestMapping(value = "/addSelectUserId", method = {RequestMethod.POST})
    @ResponseBody
    public List<Information> addDataSelect1(Locale locale, @RequestBody Information user) {
        return mainService.getUserInfoListByUserId(user.getInfoId());
    }

    /**
     * 添加用户的学分成绩信息
     *
     * @param conditionPagination 条件
     * @return
     */
    @RequiresPermissions("all")
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    @ResponseBody
    public ResultInfo add(Locale locale, @RequestBody ConditionPagination conditionPagination) {
        try {
            Information information = new Information();
            information.setCredit(Double.parseDouble(conditionPagination.getCredit()));
            information.setGrade(Double.parseDouble(conditionPagination.getGrade()));
            //int userId = mainService.getUserIdById(Integer.parseInt(conditionPagination.getUserId()));
            information.setUserId(Integer.parseInt(conditionPagination.getUserId()));
            mainService.addInformation(information);
            return new ResultUtils().success(null, I18nUtils.getrb(locale, "addSuccess"));
        } catch (Exception e) {
            return new ResultUtils().fail(I18nUtils.getrb(locale, "addFail"));
        }
    }

    /**
     * 修改用户信息
     *
     * @param userInfo 用户信息
     * @return com.study.common.entity.ResultInfo
     */
    @RequiresPermissions("all")
    @RequestMapping(value = "/updateUser", method = {RequestMethod.POST})
    @ResponseBody
    public ResultInfo updateUser(Locale locale, ConditionPagination userInfo) {
        try {
            // 判断电话号码是否存在
            User user = registerService.findByTelNumber(userInfo.getTelNumber());

            if(user == null){
            mainService.updateInfo(userInfo);
            return new ResultUtils().success(null, I18nUtils.getrb(locale, "updateSuccess"));
            }else{
                User userOne = registerService.findById(Integer.parseInt(userInfo.getInfoId()));
                if(userOne.getTelNumber().equals(userInfo.getTelNumber())){
                    mainService.updateInfo(userInfo);
                    return new ResultUtils().success(null, I18nUtils.getrb(locale, "updateSuccess"));
                }
                return new ResultUtils().fail(I18nUtils.getrb(locale, "phone"));
            }
        } catch (Exception e) {
            return new ResultUtils().fail(I18nUtils.getrb(locale, "updateFail"));
        }
    }

    /**
     * 用户信息查询
     *
     * @param locale
     * @param conditionPagination
     * @return com.study.common.entity.ResultInfo
     */
    @RequiresPermissions("all")
    @RequestMapping(value = "/addsUser", method = {RequestMethod.POST})
    @ResponseBody
    public ResultInfo addsUser(Locale locale, ConditionPagination conditionPagination) {
        try {
            return new ResultUtils().success(mainService.getPage(conditionPagination).getList(), I18nUtils.getrb(locale, "updateSuccess"));
        } catch (Exception e) {
            return new ResultUtils().fail(I18nUtils.getrb(locale, "updateFail"));
        }
    }
}
